package com.msh.artascope.devcloud.service.api;

import com.msh.artascope.devcloud.client.po.ModuleRunPo;
import com.msh.artascope.devcloud.client.qo.ModuleRunQo;
import com.msh.artascope.devcloud.service.model.query.ModuleRunQueryVo;
import com.msh.artascope.devcloud.service.model.vo.ModuleRunVo;
import com.msh.artascope.devcloud.service.model.vo.PodVo;
import com.msh.artascope.devcloud.service.service.ModuleRunService;
import com.msh.artascope.interfaces.IAccountGet;
import com.msh.artascope.interfaces.ILoginAccount;
import com.msh.frame.client.base.SortBy;
import com.msh.frame.client.common.Result;
import com.msh.frame.client.define.ColumnDef;
import com.msh.frame.client.enums.SortModeEnum;
import com.msh.frame.client.exception.BizException;
import com.msh.frame.client.validation.IListVo;
import com.msh.frame.client.validation.IUpdateVo;
import com.msh.frame.web.base.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author shihu
 * @mail m-sh@qq.com
 */
@Tag(name = "运行记录-api相关接口")
@Slf4j
@RestController
@RequestMapping("/api/moduleRun")
public class ModuleRunApi extends BaseController<ModuleRunPo, ModuleRunQo> {

  @Autowired
  private ModuleRunService moduleRunService;

  @Autowired
	private IAccountGet accountGet;


  @Operation(summary = "获取pod列表")
  @GetMapping("listPod")
  public Result<List<PodVo>> listPod(@NotNull(message = "moduleId不能为空") @RequestParam("moduleId") Long moduleId) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(moduleRunService.listPod(moduleId, loginAccount.getTenantId()));
  }

  @Operation(summary = "获取pod日志信息")
  @GetMapping("getLogs")
  public Result<String> getLogs(
      @NotNull(message = "moduleId不能为空") @RequestParam("moduleId") Long moduleId,
      @NotNull(message = "podName不能为空") @RequestParam("podName") String podName,
      @NotNull(message = "line不能为空") @RequestParam("line") Integer line
      ) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(moduleRunService.getLogs(moduleId, podName, line, loginAccount.getTenantId()));
  }

  @Operation(summary = "模块停止")
  @GetMapping("stop")
  public Result<ModuleRunVo> stop(@NotNull(message = "moduleId不能为空") @RequestParam("moduleId") Long moduleId) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(moduleRunService.stop(moduleId, loginAccount));
  }


  @Operation(summary = "模块重启")
  @GetMapping("restart")
  public Result<ModuleRunVo> restart(@NotNull(message = "moduleId不能为空") @RequestParam("moduleId") Long moduleId) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(moduleRunService.restart(moduleId, loginAccount));
  }


  @Operation(summary = "模块运行")
  @GetMapping("start")
  public Result<ModuleRunVo> start(@NotNull(message = "buildId不能为空") @RequestParam("buildId") Long buildId) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(moduleRunService.start(buildId, loginAccount));
  }

  @Operation(summary = "通用更新逻辑")
  @PostMapping("update")
  public Result update(@Validated({IUpdateVo.class,Default.class}) @RequestBody ModuleRunVo p) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    p.initLoginAccount(loginAccount);
    moduleRunService.update(p);
    return Result.successReturn();
  }


  @Operation(summary = "通用删除逻辑")
  @GetMapping("delete")
  public Result delete(@NotNull(message = "id不能为空") @RequestParam("id") Long id) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    moduleRunService.delete(id, loginAccount.getTenantId());
    return Result.successReturn();
  }

  @Operation(summary = "获取详情")
  @GetMapping("get")
  public Result<ModuleRunVo> get(@NotNull(message = "id不能为空") @RequestParam("id") Long id) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(moduleRunService.getVo(id, loginAccount.getTenantId()));
  }

  @Operation(summary = "vo列表查询")
  @PostMapping("list")
  public Result<List<ModuleRunVo>> list(@Validated({IListVo.class}) @RequestBody ModuleRunQueryVo q) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    return Result.successReturn(moduleRunService.listVo(q));
  }


  @Operation(summary = "vo列表分页查询")
  @PostMapping("page")
  public Result<List<ModuleRunVo>> page(@Validated({IListVo.class}) @RequestBody ModuleRunQueryVo q) {
    if(null == q.getSortBy()){
      q.setSortBy(new SortBy(ColumnDef.ID, SortModeEnum.DESC));
    }
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    return moduleRunService.pageVo(q);
  }

}
